package com.authine.cloudpivot.ext.applicationservice;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.authine.cloudpivot.ext.constants.AppConst;
import com.authine.cloudpivot.ext.constants.ServiceUrlConst;
import com.authine.cloudpivot.ext.dto.InviteVendorDto;
import com.authine.cloudpivot.ext.dto.OrganizationCode;
import com.authine.cloudpivot.ext.utils.HttpClientUtil;
import com.authine.hermes.app.launcher.response.SingleResponse;
import com.authine.hermes.app.launcher.service.ApplicationService;
import com.authine.mvp.app.launcher.domain.utills.ApplicationContextUtils;
import com.authine.mvp.app.launcher.dto.bo.response.BOList;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.authine.hermes.app.launcher.service.ApplicationServiceFoundation.CUSTOM_SERVICE;

@Slf4j
@Path("InviteVendorService")
public class InviteVendorService extends ApplicationService {

    private NamedParameterJdbcTemplate jdbcTemplate = ApplicationContextUtils.getBean(NamedParameterJdbcTemplate.class);

    /**
     * 原有供应商
     *
     * @param dto
     * @return
     */
    @Path("orgInviteCodeList")
    @POST
    @Operation(summary = "原有供应商", tags = CUSTOM_SERVICE, description = "id、pageIndex、pageSize必填")
    public SingleResponse orgInviteCodeList(@Parameter OrganizationCode dto) {
        String url = HttpClientUtil.getUrl(AppConst.PERSONAL_CENTER, ServiceUrlConst.ORGINVITECODELIST_URL);
        Map<String, Object> map = BeanUtil.beanToMap(dto);
        String execute = HttpClientUtil.execute(url, map);
        BOList boList = JSONObject.parseObject(execute, BOList.class);
        return  SingleResponse.of(boList.getData().get(0));
    }


    @Path("queryVendorPhone")
    @POST
    @Operation(summary = "查询供应商电话", tags = CUSTOM_SERVICE, description = "id、pageIndex、pageSize必填")
    public com.alibaba.cola.dto.SingleResponse queryVendorPhone(@Parameter InviteVendorDto dto){
        if(dto.getVendor_organization_id() == null){
            return com.alibaba.cola.dto.SingleResponse.buildFailure("-1", "供应商不能为空");
        }
        log.info("vendor_organization_id:{}",dto.getVendor_organization_id());
        Map<String,Object> parameter = new HashMap<>();
        StringBuffer sb = new StringBuffer();
        sb.append("select org.id, cellphone from personalcenter.i_personalcenter_t_organization_invite_code inviteCode ");
        sb.append( "inner join systemmanagement.i_systemmanagement_t_organization  org");
        sb.append(" on JSON_UNQUOTE(json_extract(inviteCode.vendor_organization_id,'$.id')) = org.id");
        sb.append(" where org.id = :orgId");
        parameter.put("orgId", dto.getVendor_organization_id());
        List<Map<String, Object>> resultList = jdbcTemplate.queryForList(sb.toString(), parameter);
        if (resultList != null && resultList.size() > 0 ){
            return com.alibaba.cola.dto.SingleResponse.of(resultList.get(0));
        }
        return com.alibaba.cola.dto.SingleResponse.of(null);
    }

}
